package com.cac.demo.controller.outStoreRoom;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cac.demo.controller.FinProReceive.FinProReceiveSearchRest;
import com.cac.demo.controller.backReceive.BackReceiveRest;
import com.cac.demo.controller.partReceive.PartReceiveRest;
import com.cac.demo.dao.outStoreRoom.OutStoreroomDao;
import com.cac.demo.newCode.cemop.service.Impl.CemopServiceImpl;
import com.cac.demo.newCode.materialStorage.entity.WmsDeliveryPlan;
import com.cac.demo.newCode.materialStorage.service.IWmsDeliveryPlanService;
import com.cac.demo.service.MsSslShelves.MsSslShelvesService;
import com.cac.demo.service.backReceive.BackReceiveService;
import com.cac.demo.service.msTaskPackage.MsTaskPackageService;
import com.cac.demo.service.outStoreRoom.OutStoreMaterialRoomService;
import com.cac.demo.service.outStoreRoom.OutStoreroomService;
import com.cac.demo.util.CreateCommonLog;
import com.cac.demo.util.adapter.ResponseMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/outStoreroom")
@Slf4j
public class OutStoreroomRest {
    @Autowired
    private OutStoreroomService outStoreroomService;
    @Autowired
    private MsSslShelvesService msSslShelvesService;
    @Autowired
    private CreateCommonLog createCommonLog;
    @Autowired
    private MsTaskPackageService msTaskPackageService;
    @Autowired
    private PartReceiveRest partReceiveRest;
    @Autowired
    private FinProReceiveSearchRest finProReceiveSearchRest;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private BackReceiveRest backReceiveRest;
    @Autowired
    private OutStoreroomDao outStoreroomDao;
    @Autowired
    private OutStoreMaterialRoomService outStoreMaterialRoomService;
    @Autowired
    private CemopServiceImpl cemopServiceImpl;
    @Resource
    IWmsDeliveryPlanService wmsDeliveryPlanService;
    @Autowired
    private BackReceiveService backReceiveService;

    /*零件——物料接收叫箱  此方法已经废弃*/
    @PostMapping("/partWorkbinOutStoreroom")
    public ResponseMsg<String> partWorkbinOutStoreroom(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        /*****yebo 厂内代码同步，处理多个物料 叫同一个箱问题处理,避免一个物料叫多个箱子下来 start******/
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        try {
            List<HashMap> lists = (List<HashMap>) map.get("data");
            for (HashMap tmp : lists) {
                map.put("itemCode", tmp.get("itemCode"));
                tmp.put("ip", IP);
                //当前图号的箱子是否回库
                int cc = outStoreroomDao.queryCurItemCodeWorkbinIsBack(tmp);
                HashMap tt = outStoreroomDao.queryItemReciveStatus(tmp);
                if (cc == 0) {//说明箱子已经回去，重新叫箱子
                    if (tt == null || tt.isEmpty()) {
                        outStoreroomDao.insertReceiveWorkbin(tmp);
                    }
                    break;
                }
                if (tt != null && tt.isEmpty()) {//当前相同图号已经叫了箱子，不需要重复叫箱
                    log.info("处理重复叫箱>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                    outStoreroomDao.updateReceiveWorkbin(tmp);
                    outStoreroomDao.insertWorkBinReceDetail(tmp);
                    //改变交接单状态
                    partReceiveRest.changeTransferType(map);
                    return responseMsg.setRetCode("200").setMessage("叫箱成功");
                } else {
                    outStoreroomDao.insertReceiveWorkbin(tmp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*****yebo 厂内代码同步，处理多个物料 叫同一个箱问题处理,避免一个物料叫多个箱子下来 end******/
        //事务管理
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            //改变接收状态
            partReceiveRest.changeTransferType(map);
            //调用零件缓存箱出库
            responseMsg = this.outStoreroomQuery(map);
            if (responseMsg.getRetCode().equals("201")) {
                platformTransactionManager.rollback(transaction);
                return responseMsg;
            }
            //全部成功提交事务
            platformTransactionManager.commit(transaction);
            //yebo 2023-04-08 更新接收物料信息表状态
            outStoreroomDao.updateReceiveWorkbinStatus(map);
            return responseMsg;
        } catch (NullPointerException ne) {
            ne.printStackTrace();
            platformTransactionManager.rollback(transaction);
            return responseMsg.setRetCode("201").setMessage("叫箱失败！请确认是否还有空箱！");
        } catch (Exception e) {
            e.printStackTrace();
            platformTransactionManager.rollback(transaction);
            return responseMsg.setRetCode("201").setMessage("接收失败");
        }
    }

    /*成品——物料接收叫箱  此方法已经废弃*/
    @PostMapping("/productWorkbinOutStoreroom")
    public ResponseMsg<String> productWorkbinOutStoreroom(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        /*****yebo 厂内代码同步，处理多个物料 叫同一个箱问题处理,避免一个物料叫多个箱子下来 start******/
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        try {
            List<HashMap> lists = (List<HashMap>) map.get("data");
            for (HashMap tmp : lists) {
                map.put("itemCode", tmp.get("itemCode"));
                tmp.put("ip", IP);
                //当前图号的箱子是否回库
                int cc = outStoreroomDao.queryCurItemCodeWorkbinIsBack(tmp);
                HashMap tt = outStoreroomDao.queryItemReciveStatus(tmp);
                if (cc == 0) {//说明箱子已经回去，重新叫箱子
                    if (tt == null || tt.isEmpty()) {
                        outStoreroomDao.insertReceiveWorkbin(tmp);
                    }
                    break;
                }
                if (tt != null && tt.isEmpty()) {//当前相同图号已经叫了箱子，不需要重复叫箱
                    log.info("处理重复叫箱>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                    outStoreroomDao.updateReceiveWorkbin(tmp);
                    outStoreroomDao.insertWorkBinReceDetail(tmp);
                    //改变交接单状态
                    finProReceiveSearchRest.changeTransferType(map);
                    return responseMsg.setRetCode("200").setMessage("叫箱成功");
                } else {
                    outStoreroomDao.insertReceiveWorkbin(tmp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*****yebo 厂内代码同步，处理多个物料 叫同一个箱问题处理,避免一个物料叫多个箱子下来 end******/
        //事务管理
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            finProReceiveSearchRest.changeTransferType(map);
            responseMsg = this.outStoreroomQuery(map);
            //yebo 厂内修改代码同步 成品添加物料规则没有给出提示
            if (responseMsg.getRetCode().equals("201")) {
                platformTransactionManager.rollback(transaction);
                return responseMsg;
            }
            //全部成功提交事务
            platformTransactionManager.commit(transaction);
            //yebo 2023-04-08 更新接收物料信息表状态
            outStoreroomDao.updateReceiveWorkbinStatus(map);
            return responseMsg.setRetCode("200").setMessage("接收成功");
        } catch (Exception e) {
            e.printStackTrace();
            platformTransactionManager.rollback(transaction);
            return responseMsg.setRetCode("201").setMessage("接收失败");
        }
    }

    //退库接收
    @PostMapping("/workbinOutStoreroom")
    public ResponseMsg<String> workbinOutStoreroom(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String IP=null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String userCode = (String) request.getSession().getAttribute("account");
        if("".equals(userCode)||"guest".equals(userCode)){
            return new ResponseMsg().setRetCode("201").setMessage("当前登录已过期，请重新登录！");
        }
        //事务管理
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            ArrayList<Object> lists = (ArrayList<Object>) map.get("data");
            ArrayList<HashMap> lists1 = (ArrayList<HashMap>) lists.get(0);
            HashMap hashMap = (HashMap) lists1.get(0);
            LambdaQueryWrapper<WmsDeliveryPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WmsDeliveryPlan::getWmsDeliveryPlanId, hashMap.get("wmsDeliveryPlanId"));
            WmsDeliveryPlan wmsDeliveryPlan = wmsDeliveryPlanService.getOne(queryWrapper);
            LambdaUpdateWrapper<WmsDeliveryPlan> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(WmsDeliveryPlan::getWmsDeliveryPlanId, hashMap.get("wmsDeliveryPlanId"))
                    .set(WmsDeliveryPlan::getDeliveryStatus, "3")
                    .set(WmsDeliveryPlan::getReceiveDate, new Date())
                    .set(WmsDeliveryPlan::getReceiveEmpCode, userCode)
                    .set(WmsDeliveryPlan::getLastUpdateIp, IP)
                    .set(WmsDeliveryPlan::getAttribute14, "2")
                    .set(WmsDeliveryPlan::getBePutIntoQty,new BigDecimal(wmsDeliveryPlan.getAttribute12()))
                    .set(WmsDeliveryPlan::getRetStatus,"1")
                    .set(WmsDeliveryPlan::getIp,IP);
                    wmsDeliveryPlanService.update(updateWrapper);
            log.info("退库参数：" + map.toString() + "  00");
            String account = (String) request.getSession().getAttribute("account");
            //修改交接单状态，接收回传MESx 2025-2-24尤云飞注释
//            backReceiveRest.changeBackType(map);
//            PackageUnpackService packageUnpackService = new PackageUnpackService();
            //根据交接单号查询所需参数
            try {
//                int result = backReceiveService.changeBackType(barcode, account, returnQty, IP, id, status);

                //ERP出库撤销（ERP-101-InvOutCancel）,还台帐数量，减占用数量，数据写入流水表 2025-2-24尤云飞注释
//                packageUnpackService.cancelOut(map,IP,userCode);

                //ERP占用取消（ERP-101-InvOutAllotCancel）2025-2-24尤云飞注释
//                packageUnpackService.cancelAlloment(map,IP,userCode);

            }catch (Exception e){
                e.printStackTrace();
            }
            if(wmsDeliveryPlan.getDeliveryType().equals('8')){
                //标准件叫箱子
                outStoreMaterialRoomService.callBzjBox(lists1,IP);
            }else{
                //零件成品叫箱
//                outStoreMaterialRoomService.callBox(lists1,IP);
            }
            //全部成功提交事务
            platformTransactionManager.commit(transaction);
            return responseMsg.setRetCode("200").setMessage("接收成功");
        } catch (Exception e) {
            e.printStackTrace();
            platformTransactionManager.rollback(transaction);
            return responseMsg.setRetCode("201").setMessage("接收失败");
        }
    }

    @PostMapping("/outStoreroomQuery")
    /*物料入库——叫箱*/
    public ResponseMsg<String> outStoreroomQuery(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        List<HashMap> map1 = (List<HashMap>) map.get("data");
        String operationBit = null;
        String boxType = "WORKBIN";
        if (map1.size() == 0) {
            return responseMsg.setRetCode("201").setMessage("未选中物料信息！");
        }
        //获取IP
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        //获取操作位置编号
        operationBit = msSslShelvesService.findStartCode(IP, boxType);
        for (int i = 0; i < map1.size(); i++) {

            HashMap hashmap = map1.get(i);
            hashmap.put("ip", IP);
            String itemCode = (String) hashmap.get("itemCode");
            //判断该物料是否为大件物料
            HashMap isBulky = outStoreroomService.isBulkyItems(itemCode);
            if (isBulky == null) {
                List<String> outWorkbin = outStoreroomService.outStoreroomQuery(hashmap);
                //存储过程未找到箱子
                if (outWorkbin.get(0).equals("1")) {
                    responseMsg.setRetCode("201").setMessage(outWorkbin.get(1));
                    break;
                }
                for (int j = 0; j < outWorkbin.size(); j++) {
                    String workbinCode = outWorkbin.get(j);
                    HashMap map2 = new HashMap();
                    //缓存箱编码和结束位置
                    map2.put("workbinCode", workbinCode);
                    map2.put("endCode", operationBit);
                    //通过缓存箱编码在货架上找箱子
                    HashMap out = outStoreroomService.outStoreroomWorkbin(map2);
                    //判断巷道的状态
                    String result = outStoreroomService.isRowOn(out);
                    if (result.equals("1")) {
                        //查询有没有该箱子已经在执行的任务
                        Map searchTask = outStoreroomService.searchTask(map2);
                        if (searchTask == null) {
                            if (out != null) {
                                //组装传入日志的参数
                                out.put("taskType", "2");
                                out.put("command", "1");
                                out.put("boxType", "WORKBIN");
                                out.put("IP", IP);
                                out.put("result", "receive");
                                //获取操作位置编号
                                out.put("endCode", "xyssj" + operationBit);
                                operationBit = msSslShelvesService.findStartCode(out.get("IP").toString(), (String) out.get("boxType"));
                                //创建日志
                                HashMap s = createCommonLog.outBound(out);
                                //将已经创建日志的箱子编码在货架表上标记为空:不为空箱
                                outStoreroomService.updateMsshevle(map2);
                                //responseMsg.setRetCode("200").setMessage("缓存箱出库成功").setResponseBody(s);
                                //System.out.println(">>>>>>>>>>>>responseMessage:"+responseMsg.toString());
                            } else {
                                responseMsg.setRetCode("201").setMessage("选中的物料不存在！");
                            }
                        } else {
                            if (out.get("palletCode").equals(searchTask.get("palletCode"))) {
                                continue;
                            }
                        }
                    }
                }
            } else {
                hashmap.put("ip", IP);
                outStoreroomService.insertWorkbinDetal(hashmap);
                return responseMsg.setRetCode("200").setMessage("大件货物已接收");
            }
        }
        if (responseMsg.getRetCode().equals("201")) {
            return responseMsg;
        }

        HashMap outFirst = new HashMap();
        //获取操作位编码
        operationBit = msSslShelvesService.findStartCode(IP, boxType);
        outFirst.put("endCode", "xyssj" + operationBit);
        outFirst.put("func", "receive");
        //调用流量控制类
        HashMap str = createCommonLog.callWCSInterface(outFirst);
        String s = (String) str.get("retCode");
        if (s.equals("200")) {
            return responseMsg.setRetCode("200").setMessage((String) str.get("msg"));
        } else return responseMsg.setRetCode("201").setMessage((String) str.get("msg"));
    }

    /*零件、成品退货缓存箱出库*/
    @PostMapping("/partOutStoreroom")
    @Transactional
    public ResponseMsg<String> partOutStoreroom(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        List<HashMap> map1 = (List<HashMap>) map.get("data");
        String operationBit = null;
        String boxType = "WORKBIN";
        if (map1.size() == 0) {
            return responseMsg.setRetCode("201").setMessage("未选中物料信息！");
        }
        //获取IP
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        //获取操作位置编号
        operationBit = msSslShelvesService.findStartCode(IP, boxType);
        for (int i = 0; i < map1.size(); i++) {

            HashMap hashmap = map1.get(i);

            hashmap.put("ip", IP);
            if (hashmap.get("returnStatus").equals("保管待接收")) {
                try {
                    List<String> outWorkbin = outStoreroomService.callReturnOutWorkbin(hashmap);
                    for (int j = 0; j < outWorkbin.size(); j++) {
                        String workbinCode = outWorkbin.get(j);
                        HashMap map2 = new HashMap();
                        map2.put("workbinCode", workbinCode);
                        map2.put("endCode", operationBit);
                        HashMap out = outStoreroomService.partOutStoreroom(map2);
                        //判断巷道的状态
                        String result = outStoreroomService.isRowOn(out);
                        if (result.equals("1")) {
                            //查询有没有该箱子已经在执行的任务
                            Map searchTask = outStoreroomService.searchTask(map2);
                            if (searchTask == null) {
                                //组装传入日志的参数
                                out.put("taskType", "2");
                                out.put("command", "1");
                                out.put("boxType", "WORKBIN");
                                out.put("IP", IP);
                                //获取操作位置编号
                                out.put("endCode", "xyssj" + operationBit);
                                operationBit = msSslShelvesService.findStartCode(out.get("IP").toString(), (String) out.get("boxType"));
                                //创建日志
                                HashMap s = createCommonLog.outBound(out);
                                //将已经创建日志的箱子编码在货架表上标记为空
                                outStoreroomService.updateMsshevle(map2);
                                responseMsg.setResponseBody((String) s.get("retMsg"));
                            } else {
                                if (out.get("palletCode").equals(searchTask.get("palletCode"))) {
                                    continue;
                                }
                            }
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return responseMsg.setRetCode("201");
                }
            } else return responseMsg.setRetCode("201").setMessage("请确定退货单状态！");
        }
        HashMap outFirst = new HashMap();
        //获取操作位编码
        operationBit = msSslShelvesService.findStartCode(IP, boxType);
        outFirst.put("endCode", "xyssj" + operationBit);
        outFirst.put("func", "receive");
        //调用流量控制类
        HashMap str = createCommonLog.callWCSInterface(outFirst);
        String s = (String) str.get("retCode");
        if (s.equals("200")) {
            return responseMsg.setRetCode("200").setMessage((String) str.get("msg"));
        } else return responseMsg.setRetCode("201").setMessage((String) str.get("msg"));
    }

    /*分拣管理、工艺更改缓存箱出库*/
    @Transactional
    @PostMapping("/sortStoreRoomOut")
    public ResponseMsg<String> sortStoreRoomOut(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        //获取ip
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String agv = (String) map.get("agv");
        try {
            //分拣缓存箱出库
            List<HashMap> outList = outStoreroomService.sortStoreRoomOut(map);
            if (outList.size() == 0) {
                return responseMsg.setRetCode("201").setMessage("缓存箱出库失败！");
            }
            //创建缓存箱出库日志
            for (int i = 0; i < outList.size(); i++) {
                HashMap out = outList.get(i);
                //判断巷道的状态
                String operationBit = null;
                String boxType = "WORKBIN";
                //获取操作位置编号
                operationBit = msSslShelvesService.findStartCode(IP, boxType);
                HashMap outPa = new HashMap();
                outPa.put("workbinCode", out.get("palletCode"));
                //通过缓存箱编码在货架上找箱子
                HashMap map2 = outStoreroomService.outStoreroomWorkbin(outPa);
                map2.put("workbinCode", out.get("palletCode"));
                map2.put("endCode", operationBit);
                //判断巷道状态
                String result = outStoreroomService.isRowOn(map2);
                if (result.equals("1")) {
                    if (out.get("palletCode").toString().startsWith("1")) {
                        out.put("boxType", "WORKBIN");
                        out.put("taskType", "2");
                        out.put("command", "1");
                        out.put("IP", IP);
                        out.put("startCode", map2.get("startCode"));
                        out.put("roadway", map2.get("roadway"));
                        //获取操作位置编号
                        out.put("endCode", "xyssj" + operationBit);
                        //创建日志
                        HashMap s = createCommonLog.outBound(out);
                    } else return responseMsg.setRetCode("201").setMessage("缓存箱条码格式错误！");
                }
            }
            //工作包出库，获得托盘编码
            List<HashMap> mapList = msTaskPackageService.taskOut(map);
            if (mapList.size() == 0) {
                return responseMsg.setRetCode("201").setMessage("无法下达工作包出库命令！");
            }
            //创建工作包出库日志
            for (int i = 0; i < mapList.size(); i++) {
                String operationBit = null;
                String boxType = "TOOL";
                //获取操作位置编号
                operationBit = msSslShelvesService.findStartCode(IP, boxType);
                HashMap outPackage = mapList.get(i);
                HashMap outPa = new HashMap();
                outPa.put("workbinCode", outPackage.get("palletCode"));
                //通过缓存箱编码在货架上找箱子
                HashMap map2 = outStoreroomService.outStoreroomWorkbin(outPa);
                if (outPackage.get("palletCode").toString().startsWith("2")) {
                    outPackage.put("boxType", "TOOL");
                    outPackage.put("taskType", "2");
                    outPackage.put("command", "1");
                    outPackage.put("IP", IP);
                    outPackage.put("startCode", map2.get("startCode"));
                    outPackage.put("roadway", map2.get("roadway"));
                    //获取操作位置编号
                    outPackage.put("endCode", "xyssj" + operationBit);

                    if (agv.equals("agv")) {
                        outPackage.put("agv", agv);
                    }
                    //创建日志
                    HashMap s = createCommonLog.outBound(outPackage);
                } else return responseMsg.setRetCode("201").setMessage("工作包条码格式错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return responseMsg.setRetCode("201");
        }
        //调用流量控制--缓存箱
        HashMap outFirst = new HashMap();
        String operationBit = null;
        String boxType = "WORKBIN";
        //获取操作位
        operationBit = msSslShelvesService.findStartCode(IP, boxType);
        outFirst.put("endCode", "xyssj" + operationBit);
        outFirst.put("func", "sort");
        outFirst.put("boxType", "WORKBIN");
        //调用流量控制类
        HashMap s = createCommonLog.callWCSInterface(outFirst);
        //调用流量控制--工作包
        HashMap outFirstPackage = new HashMap();
        String operationBitPackage = null;
        String boxTypePackage = "TOOL";
        //获取操作码
        operationBitPackage = msSslShelvesService.findStartCode(IP, boxTypePackage);
        outFirstPackage.put("endCode", "xyssj" + operationBitPackage);
        outFirstPackage.put("func", "sort");
        outFirstPackage.put("boxType", "TOOL");
        //调用流量控制类
        HashMap s1 = createCommonLog.callWCSInterface(outFirstPackage);
        String str = (String) s1.get("retCode");
        if (str.equals("200")) {
            return responseMsg.setRetCode("200").setMessage((String) s1.get("msg"));
        } else return responseMsg.setRetCode("201").setMessage((String) s1.get("msg"));

    }

    /*开启巷道*/
    @Transactional
    @PostMapping("/rowTurnOn")
    public ResponseMsg<String> rowTurnOn(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        map.put("status", "1");
        try {
            outStoreroomService.rowTurn(map);
            return responseMsg.setRetCode("200").setMessage("开启成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return responseMsg.setRetCode("201").setMessage("开启失败！");
        }
    }

    /*关闭巷道*/
    @Transactional
    @PostMapping("/rowTurnDown")
    public ResponseMsg<String> rowTurnDown(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        map.put("status", "2");
        try {
            outStoreroomService.rowTurn(map);
            return responseMsg.setRetCode("200").setMessage("关闭成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return responseMsg.setRetCode("201").setMessage("关闭失败！");
        }
    }

    //手动完成正在执行的任务
    @Transactional
    @PostMapping("/taskFinish")
    public ResponseMsg<Integer> taskFinish(@RequestBody HashMap map) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        try {

            List<String> msGkLogIds = (List<String>) map.get("msGkLogIds");
            if(msGkLogIds.size()==0){
                return responseMsg.setRetCode("201").setMessage("请选择一条数据！");
            }
            for (int i = 0; i < msGkLogIds.size(); i++) {
                String msGkLogId = msGkLogIds.get(i);
                HashMap hashMap = new HashMap();
                hashMap.put("msGkLogId", msGkLogId);
                outStoreroomService.taskFinish(hashMap);
            }
            return responseMsg.setRetCode("200");
        } catch (Exception e) {
            e.printStackTrace();
            return responseMsg.setRetCode("201").setMessage("失败");
        }
    }

    //手动清理当前位置流量
    @Transactional
    @PostMapping("/isBackFinish")
    public HashMap isBackFinish(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        try {
            List<String> msGkLogIds = (List<String>) map.get("msGkLogIds");
            for (String msGkLogId : msGkLogIds) {
                outStoreroomDao.isBackFinish(msGkLogId);
            }
            hashMap.put("retCode", "200");
            hashMap.put("retMsg", "清理成功！");
            return hashMap;
        } catch (Exception e) {
            e.printStackTrace();
            hashMap.put("retCode", "201");
            hashMap.put("retMsg", "清理失败，请重试！");
            return hashMap;
        }
    }

}
